package com.fc.v2.controller.gen;

import com.fc.v2.common.base.BaseController;
import com.fc.v2.common.domain.AjaxResult;
import com.fc.v2.common.domain.ResultTable;
import com.fc.v2.mapper.auto.ApplyProjectMapper;
import com.fc.v2.model.Examine;
import com.fc.v2.model.auto.*;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.service.ApplyParticipatorService;
import com.fc.v2.service.ApplyProjectService;
import com.fc.v2.service.FinishProjectService;
import com.fc.v2.service.SysPositionService;
import com.fc.v2.shiro.util.ShiroUtils;
import com.fc.v2.util.SnowflakeIdWorker;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.fc.v2.common.conf.oss.OssConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * 结项审核申请Controller
 * @ClassName: FinishProjectController
 * @author Archie
 * @date 2021-11-08 11:54:59
 */
@Api(value = "结项审核申请")
@Controller
@RequestMapping("/FinishProjectController")
public class FinishProjectController extends BaseController{
	
	private String prefix = "gen/finishProject";
	
	@Autowired
	private FinishProjectService finishProjectService;

	@Autowired
	private ApplyProjectService applyProjectService;

	@Autowired
	private SysPositionService sysPositionService;

	@Autowired
	private ApplyParticipatorService applyParticipatorService;
	
	
	/**
	 * 结项审核申请页面展示
	 * @param model
	 * @return String
	 * @author Archie
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
	@RequiresPermissions("gen:finishProject:view")
    @OssConfig
    public String view(ModelMap model)
    {
        return prefix + "/list";
    }
	
	/**
	 * list集合
	 * @param tablepar
	 * @param searchText
	 * @return
	 */
	//@Log(title = "结项审核申请", action = "111")
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/list")
	@RequiresPermissions("gen:finishProject:list")
	@ResponseBody
	public ResultTable list(Tablepar tablepar,FinishProject finishProject){
		PageInfo<FinishProject> page=finishProjectService.list(tablepar,finishProject) ; 
		return pageTable(page.getList(),page.getTotal());
	}
	
	/**
     * 新增跳转
     */
	@ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    @OssConfig
    public String add(Model model)
    {

		//获取当前用户Id
		String userId = ShiroUtils.getUserId();
		TsysUser tsysUser = sysUserService.selectByPrimaryKey(userId);
		model.addAttribute("TsysUser", tsysUser);

		//获取单位
		String deptName = tsysUser.getDeptName();
		//获取单位管理模块信息
		SysPositionExample testExample=new SysPositionExample();
		testExample.createCriteria().andDeptNameEqualTo(deptName);
		List<SysPosition> sysPositionList = new ArrayList<>();
		sysPositionList = sysPositionService.selectByExample(testExample);
		SysPosition sysPosition = sysPositionList.get(0);
		model.addAttribute("SysPosition",sysPosition);

		//获取立项项目信息
		ApplyProjectExample example = new ApplyProjectExample();
		example.createCriteria().andManIdEqualTo(ShiroUtils.getUserId()).andProStateEqualTo(Examine.PASSED.code);
		List<ApplyProject> applyProjectList = applyProjectService.selectByExample(example);
//		FinishProjectExample example2 = new FinishProjectExample();
//		example2.createCriteria().andManIdEqualTo(ShiroUtils.getUserId()).andProStateEqualTo(Examine.NEED_EXAMINE.code);
		model.addAttribute("applyProjectList",applyProjectList);

		//获取课题参与者信息
		//添加结项雪花主键id
		String proId =  SnowflakeIdWorker.getUUID();
		model.addAttribute("proId", proId);
		//查询当前用户的项目id
		List<ApplyProject> apList = new ArrayList<>();
		ApplyProjectExample apExample = new ApplyProjectExample();
		//复制立项的课题参与者，添加立项id和结项id
		for (ApplyProject ap : applyProjectList) {
			List<ApplyParticipator> appList = new ArrayList<>();
			ApplyParticipatorExample appExample = new ApplyParticipatorExample();
			appExample.createCriteria().andProjectIdEqualTo(ap.getId()).andFinishIdIsNull();
			appList = applyParticipatorService.selectByExample(appExample);
			for (ApplyParticipator app:appList) {
				app.setFinishId(proId);
				applyParticipatorService.insertSelective(app);
			}
		}

        return prefix + "/add";
    }

	/**
	 * list集合
	 * @param tablepar
	 * @param searchText
	 * @return
	 * @author fuce
	 * @Date 2019年11月11日 下午4:14:40
	 */
	@GetMapping("/list2")
	@ResponseBody
	public AjaxResult list(){
		AjaxResult ajaxResult=new AjaxResult();

		//获取立项项目信息
		ApplyProjectExample example = new ApplyProjectExample();
		example.createCriteria().andManIdEqualTo(ShiroUtils.getUserId()).andProStateEqualTo(Examine.PASSED.code);
		List<ApplyProject> applyProjectList = applyProjectService.selectByExample(example);
		ajaxResult.put("val",applyProjectList);

		return ajaxResult;
	}

    /**
     * 新增提交
     * @param 
     * @return
     */
	//@Log(title = "结项审核申请新增", action = "111")
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/add")
	@RequiresPermissions("gen:finishProject:add")
	@ResponseBody
	public AjaxResult add(FinishProject finishProject,@RequestParam("id") String id){
		int b=finishProjectService.insertSelectiveSub(finishProject,id);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}

	/**
	 * 新增保存
	 * @param
	 * @return
	 */
	//@Log(title = "结项审核申请新增", action = "111")
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/addSave")
	@RequiresPermissions("gen:finishProject:add")
	@ResponseBody
	public AjaxResult addSave(FinishProject finishProject,@RequestParam("id") String id){
		int b=finishProjectService.insertSelectiveSave(finishProject,id);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 结项审核申请删除
	 * @param ids
	 * @return
	 */
	//@Log(title = "结项审核申请删除", action = "111")
	@ApiOperation(value = "删除", notes = "删除")
	@DeleteMapping("/remove")
	@RequiresPermissions("gen:finishProject:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=finishProjectService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}

	/**
	 * 删除未提交的课题参与者信息
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "删除", notes = "删除")
	@DeleteMapping("/removeParticipator")
	@RequiresPermissions("gen:finishProject:remove")
	@ResponseBody
	public void removeParticipator(String ids,@PathVariable("proId") String proId){
		FinishProjectExample example =new FinishProjectExample();
		example.createCriteria().andManIdEqualTo(ShiroUtils.getUserId());
		List<FinishProject> finishProjectList = new ArrayList<>();
		finishProjectList = finishProjectService.selectByExample(example);
		int n = 0;
		for (FinishProject fp:finishProjectList) {
			if(!proId.equals(fp.getId())){
				n++;
			}
		}
		if(n == finishProjectList.size()){
			ApplyParticipatorExample appExample = new ApplyParticipatorExample();
			appExample.createCriteria().andProjectIdEqualTo(proId);
			applyParticipatorService.deleteByExample(appExample);
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{id}")
    @OssConfig
    public String edit(@PathVariable("id") String id, Model model)
    {
        model.addAttribute("FinishProject", finishProjectService.selectByPrimaryKey(id));

		//获取当前用户Id
		String userId = ShiroUtils.getUserId();
		TsysUser tsysUser = sysUserService.selectByPrimaryKey(userId);
		model.addAttribute("TsysUser", tsysUser);

		//获取单位
		String deptName = tsysUser.getDeptName();
		//获取单位管理模块信息
		SysPositionExample testExample=new SysPositionExample();
		testExample.createCriteria().andDeptNameEqualTo(deptName);
		List<SysPosition> sysPositionList = new ArrayList<>();
		sysPositionList = sysPositionService.selectByExample(testExample);
		SysPosition sysPosition = sysPositionList.get(0);
		model.addAttribute("SysPosition",sysPosition);

        return prefix + "/edit";
    }

	/**
	 * 查看跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/check/{id}")
	@OssConfig
	public String check(@PathVariable("id") String id, Model model)
	{
		model.addAttribute("FinishProject", finishProjectService.selectByPrimaryKey(id));

		//获取当前用户Id
		String userId = ShiroUtils.getUserId();
		TsysUser tsysUser = sysUserService.selectByPrimaryKey(userId);
		model.addAttribute("TsysUser", tsysUser);

		//获取单位
		String deptName = tsysUser.getDeptName();
		//获取单位管理模块信息
		SysPositionExample testExample=new SysPositionExample();
		testExample.createCriteria().andDeptNameEqualTo(deptName);
		List<SysPosition> sysPositionList = new ArrayList<>();
		sysPositionList = sysPositionService.selectByExample(testExample);
		SysPosition sysPosition = sysPositionList.get(0);
		model.addAttribute("SysPosition",sysPosition);

		return prefix + "/check";
	}

	/**
     * 修改保存
     */
    //@Log(title = "结项审核申请修改", action = "111")
	@ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:finishProject:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSubmmit(FinishProject finishProject)
    {
        return toAjax(finishProjectService.updateByPrimaryKeySelective(finishProject));
    }

	/**
	 * 修改保存
	 */
	//@Log(title = "结项审核申请修改", action = "111")
	@ApiOperation(value = "修改保存", notes = "修改保存")
	@RequiresPermissions("gen:finishProject:edit")
	@PostMapping("/editSave")
	@ResponseBody
	public AjaxResult editSave(FinishProject finishProject)
	{
		return toAjax(finishProjectService.updateByPrimaryKeySelectiveSave(finishProject));
	}

    /**
	 * 修改状态
	 * @param record
	 * @return
	 */
    @PutMapping("/updateVisible")
	@ResponseBody
    public AjaxResult updateVisible(@RequestBody FinishProject finishProject){
		int i=finishProjectService.updateVisible(finishProject);
		return toAjax(i);
	}

//	/**
//	 * 查询选择项目信息
//	 * @param id
//	 * @param mmap
//	 * @return
//	 */
//	@ApiOperation(value = "修改跳查询选择项目信息", notes = "修改跳查询选择项目信息")
//	@GetMapping("/applyInfo")
//	public void edit(ModelMap map,@RequestParam("applyId") String applyId)
//	{
//		map.put("ApplyProjectSelect", applyProjectService.selectByPrimaryKey(applyId));
//
//	}





	
}
